package com.wanmait.travel.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wanmait.travel.dao.PriceDAO;
import com.wanmait.travel.dao.ScenicDAO;
import com.wanmait.travel.vo.Scenic;


@WebServlet("/SreachServlet")
public class SreachServlet extends HttpServlet {
	private ScenicDAO scenicDAO= ScenicDAO.getDAO();
	private PriceDAO priceDAO= PriceDAO.getDAO();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String action= request.getParameter("action");
		
		if (action == null) {
			response.sendRedirect("/Travel");
			return;
		}
		switch(action){
		case "sreachName":
			this.sreachName(request, response);
			break;
		case "sreachPrice":
			this.sreachPrice(request, response);
			break;
		case "sreachMax":
			this.sreachMax(request, response);
			break;
		case "sreachMin":
			this.sreachMin(request, response);
			break;
		}
	}
	protected void sreachName(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String travel =request.getParameter("travel");
		List<Scenic> scenics= scenicDAO.findByName(travel);
	
		request.setAttribute("scenics",scenics );
		request.getRequestDispatcher("/scenic/scenicListShow.jsp").forward(request, response);
	}
   protected void sreachPrice(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	   request.setCharacterEncoding("UTF-8");
	   String priceOstr=request.getParameter("price_o");
	   String priceTstr=request.getParameter("price_t");
	   
	   double priceO=0;
	   try {
		    priceO=Double.parseDouble(priceOstr);
	} catch (Exception e) {
		// TODO: handle exception
	}
	   double priceT=999999;
	   try {
		   priceT=Double.parseDouble(priceTstr);
	} catch (Exception e) {
		// TODO: handle exception
	}
	   List<Scenic> scenics=priceDAO.findByPrice(priceO, priceT);
	   request.setAttribute("scenics", scenics);
	   request.getRequestDispatcher("/scenic/scenicListShow.jsp").forward(request, response);
	  
		
	}
   protected void sreachMax(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	   List<Scenic> scenics=priceDAO.findByMax();
		request.setAttribute("scenics",scenics );
		request.getRequestDispatcher("/ajax/scenicListShow.jsp").forward(request, response);
	}
   protected void sreachMin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	   List<Scenic> scenics=priceDAO.findByMin();
		request.setAttribute("scenics",scenics );
		request.getRequestDispatcher("/ajax/scenicListShow.jsp").forward(request, response);
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doGet(request, response);
	}

}
